package com.originskyseed.platform.cms.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Component;

import com.originskyseed.platform.cms.mapper.ComplainAndSuggestMapper;
import com.originskyseed.platform.cms.mapper.ComplainAndSuggestRelReplyMapper;
import com.originskyseed.platform.cms.model.ComplainAndSuggest;
import com.originskyseed.platform.cms.model.ComplainAndSuggestRelReply;
import com.originskyseed.platform.cms.service.IComplainAndSuggestService;
import com.originskyseed.platform.core.base.BasePage;
import com.originskyseed.platform.core.util.bean.PrimaryKeyGeneratorUtil;
import com.originskyseed.platform.user.model.UserInfo;

/**
 * 投诉与建议表Service接口实现类（OSI_COMPLAIN_AND_SUGGEST）
 *
 * @author WuZhuoXi
 */
@DubboService
@Component("complainAndSuggestService")
public class ComplainAndSuggestServiceImpl implements IComplainAndSuggestService {

	@Resource
	private ComplainAndSuggestMapper complainAndSuggestMapper;

	@Resource
	private ComplainAndSuggestRelReplyMapper complainAndSuggestRelReplyMapper;

	/**
	 * 删除记录
	 *
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@Override
	public int deleteByPrimaryKey(String id) throws Exception {
		return this.complainAndSuggestMapper.deleteByPrimaryKey(id);
	}

	/**
	 * 批量删除记录
	 *
	 * @param ids
	 * @return
	 * @throws Exception
	 */
	@Override
	public int deleteByPrimaryKeys(List<String> ids) throws Exception {
		return this.complainAndSuggestMapper.deleteByPrimaryKeys(ids);
	}

	/**
	 * 插入记录
	 *
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int insert(ComplainAndSuggest record) throws Exception {
		if (StringUtils.isBlank(record.getId())) {
			record.setId(PrimaryKeyGeneratorUtil.getInstance().getRandomKey());
		}
		record.setCreateTime(new Date());
		return this.complainAndSuggestMapper.insert(record);
	}

	/**
	 * 插入记录（仅非空值字段）
	 *
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int insertSelective(ComplainAndSuggest record) throws Exception {
		if (StringUtils.isBlank(record.getId())) {
			record.setId(PrimaryKeyGeneratorUtil.getInstance().getRandomKey());
		}
		record.setCreateTime(new Date());
		return this.complainAndSuggestMapper.insertSelective(record);
	}

	/**
	 * 更新记录
	 *
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int updateByPrimaryKey(ComplainAndSuggest record) throws Exception {
		record.setUpdateTime(new Date());
		return this.complainAndSuggestMapper.updateByPrimaryKey(record);
	}

	/**
	 * 更新记录（仅非空值字段）
	 *
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int updateByPrimaryKeySelective(ComplainAndSuggest record, UserInfo userInfo) throws Exception {
		record.setUpdateTime(new Date());
		this.complainAndSuggestMapper.updateByPrimaryKeySelective(record);
		ComplainAndSuggestRelReply complainAndSuggestRelReply = new ComplainAndSuggestRelReply();
		complainAndSuggestRelReply.setId(PrimaryKeyGeneratorUtil.getInstance().getRandomKey());
		complainAndSuggestRelReply.setComplainDescribe(record.getComplainContent());
		complainAndSuggestRelReply.setSuggestId(record.getId());
		complainAndSuggestRelReply.setReplyId(userInfo == null ? "" : userInfo.getId());
		complainAndSuggestRelReply.setReplyName(userInfo == null ? "" : userInfo.getName());
		complainAndSuggestRelReply.setCreator(userInfo == null ? "" : userInfo.getName());
		complainAndSuggestRelReply.setCreatorId(userInfo == null ? "" : userInfo.getId());
		complainAndSuggestRelReply.setCreateTime(new Date());
		complainAndSuggestRelReply.setIsDel(0);
		return complainAndSuggestRelReplyMapper.insertSelective(complainAndSuggestRelReply);

	}

	/**
	 * 查询记录
	 *
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@Override
	public ComplainAndSuggest selectByPrimaryKey(String id) throws Exception {
		return this.complainAndSuggestMapper.selectByPrimaryKey(id);
	}

	/**
	 * 查询列表
	 *
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public List<ComplainAndSuggest> selectList(ComplainAndSuggest record) throws Exception {
		return this.complainAndSuggestMapper.selectList(record);
	}

	/**
	 * 分页查询列表
	 *
	 * @param params
	 * @param page
	 * @return
	 * @throws Exception
	 */
	@Override
	public BasePage<ComplainAndSuggest> selectListByPage(Map<String, Object> params, BasePage<ComplainAndSuggest> page) throws Exception {
		params.put("page", page);
		List<ComplainAndSuggest> list = this.complainAndSuggestMapper.selectListByPage(params);
		page.setResults(list);
		return page;
	}

	/**
	 * 分页查询列表
	 *
	 * @param params
	 * @param page
	 * @return
	 * @throws Exception
	 */
	public BasePage<ComplainAndSuggest> selectListByForwardIdByPage(Map<String, Object> params, BasePage<ComplainAndSuggest> page) throws Exception {
		params.put("page", page);
		List<ComplainAndSuggest> list = this.complainAndSuggestMapper.selectListByForwardIdByPage(params);
		page.setResults(list);
		return page;
	}
}
